class Audio {
	audioContext = null // 全局音频实例
	pre_this = null // 承载上一个音频的实例vue
	
	setPre_this(data) {
	    this.pre_this = data;
	}
	setAudioContext(data) {
	    this.audioContext = data;
	}
	
	// 创建音频播放实例
	createAudio() {
	    if (this.audioContext) {
	        this.destroyAudio();
	    }
		this.setAudioContext(uni.createInnerAudioContext())
	}
	// 销毁
	destroyAudio() {
	    if (this.audioContext) {
	        this.audioContext.destroy();
	        this.setAudioContext(null);
	    }
	}
	// 播放
	playAudio(_this, src) {
		this.createAudio();
	    if (_this.audioPlaying) { // 如果当前音频增在播放，则停止
			_this.audioPlaying = false;
	        this.audioContext.stop();
	        return;
	    }
	    if (this.pre_this) { // 如果有上一个vue实例
	        this.pre_this.audioPlaying = false;
	    }
	    this.setPre_this(_this); // 保存当前播放音频的vue实例
		// 停止上一个音频播放
	    this.audioContext.stop();
		// 取消所有监听事件，销毁重建音频实例
	    this.audioContext.offPlay();
	    this.audioContext.offStop();
	    this.audioContext.offEnded();
	    this.destroyAudio();
	    this.createAudio();
	    // 监听：播放录音
	    this.audioContext.onPlay(() => {
	        _this.audioPlaying = true;
	    });
	    // 监听：播放暂停
	    this.audioContext.onPause(() => {
	        _this.audioPlaying = false;
	    });
		// 监听：播放停止
		this.audioContext.onStop(() => {
		    _this.audioPlaying = false;
		});
	    // 监听：播放自然停止
	    this.audioContext.onEnded(() => {
	        _this.audioPlaying = false;
	    });
	    this.audioContext.src = src;
	    this.audioContext.play();
	}
}

export default new Audio()